package cn.zcf.org.service.impl;

import cn.zcf.basic.service.impl.BaseServiceImpl;
import cn.zcf.org.domain.Emp;
import cn.zcf.org.mapper.EmpMapper;
import cn.zcf.org.service.IEmpService;
import cn.zcf.user.mapper.LogininfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmpServiceImpl extends BaseServiceImpl<Emp> implements IEmpService {
    @Autowired
    private EmpMapper mapper;
/*    @Autowired
    private LogininfoMapper logininfoMapper;*/
    @Override
    public void remove(Long id) {
        Emp emp = mapper.loadById(id);
/*        //删除登录信息表数据
        logininfoMapper.remove(emp.getLogininfo_id());*/

        //删除t_emp:删除中间表信息：t_emp_role
        mapper.removeEmployeeRole(id);

        //删除emp表数据
        mapper.remove(id);
    }
    @Override
    public void add(Emp emp) {
        // 保存emp表
        mapper.add(emp);  // 返回自增的主键

        // 保存中间表
        mapper.saveEmployeeRole(emp.getId(),emp.getRole_id());
    }

    @Override
    public void update(Emp emp) {

        super.update(emp);

        // 根据id删除t_emp_role
        mapper.removeEmployeeRole(emp.getId());

        // 再加一波
        mapper.saveEmployeeRole(emp.getId(),emp.getRole_id());

    }

    @Override
    public List<Emp> loadByShopId(Long shopId) {
        return mapper.loadByShopId(shopId);
    }
}
